package com.graphhopper.util.buffer;


import org.apache.commons.lang3.StringUtils;
import org.locationtech.jts.geom.Geometry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 兴趣点围栏类型
 *
 * @author heguanglong
 * @date 2020-12-02
 */
public enum PoiTypeEnums {

    /**
     * 圆形
     */
    DEFAULT(-1, "圆形", "circle", new DefaultBufferImpl()),
    /**
     * 矩形
     */
    CIRCLE(1, "圆形", "circle", new DefaultBufferImpl()),
    /**
     * 矩形
     */
    RECTANGLE(2, "矩形", "rectangle", new EnvelopeBufferImpl()),

    /**
     * 多边形
     */
    POLYGON(3, "多边形", "polygon", new PolygonBufferImpl()),
    /**
     * 线形
     */
    POLYLINE(4, "线形", "polyline", new LineBufferImpl()),

    /**
     * 行政区域围栏
     */
    AD_CODE(5, "行政区域围栏", "adcode", new PolygonBufferImpl());

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public BufferStrategy getBufferUtil() {
        return bufferUtil;
    }

    public void setBufferUtil(BufferStrategy bufferUtil) {
        this.bufferUtil = bufferUtil;
    }

    public static Map<String, PoiTypeEnums> getPoiTypeEnumsMap() {
        return poiTypeEnumsMap;
    }

    public static void setPoiTypeEnumsMap(Map<String, PoiTypeEnums> poiTypeEnumsMap) {
        PoiTypeEnums.poiTypeEnumsMap = poiTypeEnumsMap;
    }

    private String code;
    private BufferStrategy bufferUtil;

    PoiTypeEnums(Integer id, String name, String code, BufferStrategy bufferUtil) {
        this.name = name;
        this.id = id;
        this.code = code;
        this.bufferUtil = bufferUtil;
    }

    private static Map<String, PoiTypeEnums> poiTypeEnumsMap = new ConcurrentHashMap<>();

    static {
        addMap();
    }

    public static String addBuffByCode(String gps, String code, Double buffer) {
        if (StringUtils.isEmpty(gps) || buffer == null || buffer <= 0) {
            return gps;
        }
        PoiTypeEnums poiTypeEnums = poiTypeEnumsMap.get(code);
        if (poiTypeEnums != null) {
            return poiTypeEnums.getBufferUtil().addBuffer(gps, buffer);
        }
        return PoiTypeEnums.DEFAULT.bufferUtil.addBuffer(gps, buffer);
    }

    private static void addMap() {
        for (PoiTypeEnums typeEnums : values()) {
            poiTypeEnumsMap.put(typeEnums.getCode(), typeEnums);
        }
    }

    public static String geometryInfo(String gps, String code, Double buffer) {
        if (StringUtils.isEmpty(gps) || buffer == null ) {
            return gps;
        }
        PoiTypeEnums poiTypeEnums = poiTypeEnumsMap.get(code);
        if (poiTypeEnums != null) {
            return poiTypeEnums.getBufferUtil().geometryInfo(gps, buffer);
        }
        return PoiTypeEnums.DEFAULT.bufferUtil.addBuffer(gps, buffer);
    }

    public static Geometry geometry(String gps, String code) {
        if (StringUtils.isEmpty(gps) ) {
            return null;
        }
        PoiTypeEnums poiTypeEnums = poiTypeEnumsMap.get(code);
        if (poiTypeEnums != null) {
            return poiTypeEnums.getBufferUtil().geometry(gps);
        }
        return null;
    }
}
